# example: three tables: books, authors, comments.
# * a book has one author (has_one)
# * a book has many comments (has_many)

book_graph = DB[:books].object_graph do
  has_one :author, :table => :authors, :key => :author_id, :remote_key => :id
  has_many :comments, :table => :comments, :key => :id, :remote_key => :book_id
end

author_graph = DB[:authors].object_graph do
  has_many :books, :table => :books, :key => :id, :remote_key => :author_id
  has_many :comments, :table => :comments, :key => :books__id, :remote_key = :book_id
end

comment_graph = DB[:comments].object_graph do
  has_one :book, :table => :books, :key => :book_id, :remote_key => :id
  has_one :author, :table => :authors, :key => :books__author_id, :remote_key => :id
end

# let's imagine a graph class that generates the object graphs
graph = Sequel::Graph.new(DB) do
  one_to_many :books, :comments, :id => :book_id
  one_to_many :authors, :books, :id => :author_id
end

books = graph[:books] #=> returns a Dataset
books.filter(:books__id => 30).load #=> loads the graph

# in the context of a model class:
class Author < Sequel::Model
end

class Book < Sequel::Model
  has_one :author # implies many_to_one
  has_many :comments # implies one_to_many
  
  eager_load :author, :comments
end

b = Book[1] # includes author, comments